-- AccompanyDisplayM
-- Create by dengc Oct/26/2016
-- 伴生物外观模块

module("AccompanyDisplayM", package.seeall);

local accompanyDisplayTable = {};

-- 载入配置表
local _init = false;
local loadCsv = function()
    if _init then return end
    _init = true;

    accompanyDisplayTable = {};

    -- 转换extra_monster_CSV表
    local accompany_display_CSV = get_accompany_display_CSV();
    for i = 1, #accompany_display_CSV do
        local id = accompany_display_CSV[i].id;
        accompanyDisplayTable[id] = accompany_display_CSV[i];

        -- 解析动作帧范围
        parseActionFrameRange(accompany_display_CSV[i]);
    end
    accompany_display_CSV = {};
end

-- 模块初始化
function init()
    loadCsv();

    EventMgr.removeAll("AccompanyDisplayM");
end

-- 检索某个怪物的配置数据
function query(id, path)
    local m = accompanyDisplayTable[id]
    if not m or path == "" or not path then
        return m;
    end

    return m[path];
end

-- 解析动作的帧范围
function parseActionFrameRange(info)
    -- 待机、受创、死亡
    local fields = { "idle_frame", "damaged_frame", "die_frame", };

    for _, field in pairs(fields) do
        local range = info[field];
        if not string.startWith(range, "[") or not string.endWith(range, "]") then
            error(string.format("字段(%s)配置格式错误!", tostring(field)));
            return info;
        end

        local arr = string.explode(string.sub(range, 2, -2),",");
        if #arr ~= 2 then
            error(string.format("字段(%s)配置格式错误!", tostring(field)));
            return info;
        end

        info[field] = { tonumber(arr[1]), tonumber(arr[2]) };
    end

    return info;
end
